﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.AssignDataFromTC.BLDataGrid
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using DnProcessSimulateCommands.AssignDataFromTC.ADTCExtensions;
using DnProcessSimulateCommands.AssignDataFromTC.BLModel;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Media;
using Tecnomatix.Engineering.Ui.WPF;


namespace DnProcessSimulateCommands.AssignDataFromTC;

public class BLDataGrid : DataGrid
{
  private bool _hasFilter;
  private List<BomLine> _bomLines = new List<BomLine>();

  public int NumberOfStaticColumns { get; set; } = 3;

  public int FirstStaticColumnIndex { get; set; } = 1;

  public void AddTextColumn(int index, DataGridTextColumn textCol)
  {
    this.Columns.Add((DataGridColumn) textCol);
  }

  public void RemoveTextColumns()
  {
    if (this.Columns.Count <= this.NumberOfStaticColumns)
      return;
    int num = this.NumberOfStaticColumns - 1;
    for (int index = this.Columns.Count - 1; index >= 0 && index > num; --index)
      this.Columns.RemoveAt(index);
  }

  public void AddPropertyColumns()
  {
    BomLineModel dataContext = this.DataContext as BomLineModel;
    for (int index = 0; index < dataContext.PropertyNiceNames.Count; ++index)
    {
      DataGridTextColumn dataGridTextColumn = new DataGridTextColumn();
      dataGridTextColumn.Binding = (BindingBase) new Binding($"PropertyValues[{index}]");
      dataGridTextColumn.SortMemberPath = $"PropertyValues[{index}]";
      dataGridTextColumn.IsReadOnly = true;
      dataGridTextColumn.CanUserResize = true;
      dataGridTextColumn.HeaderTemplate = this.FindResource((object) "BLHeaderTemplate") as DataTemplate;
      dataGridTextColumn.Header = (object) dataContext.PropertyNiceNames[index];
      this.Columns.Add((DataGridColumn) dataGridTextColumn);
    }
  }

  public int GetColumnIndex(FilterButton button)
  {
    DataGridColumnHeader vtAncestor = ADTCExtension.GetVTAncestor<DataGridColumnHeader>((object) button);
    return vtAncestor == null ? this.FirstStaticColumnIndex : vtAncestor.DisplayIndex;
  }

  public void SetObjectVisibility(int rowIndex, TxVisibilityState objVisibility)
  {
    if (this._hasFilter)
    {
      if (this._bomLines.Count <= rowIndex)
        return;
      this._bomLines[rowIndex].ObjVisibility = objVisibility;
    }
    else
    {
      BomLineModel dataContext = this.DataContext as BomLineModel;
      if (dataContext.BomLines.Count <= rowIndex)
        return;
      dataContext.BomLines[rowIndex].ObjVisibility = objVisibility;
    }
  }

  public void ClearBomLineData()
  {
    this._hasFilter = false;
    (this.DataContext as BomLineModel).BomLines.Clear();
    this.HandleFilterRemoved();
    this._bomLines.Clear();
  }

  public void CleanUpFilterData()
  {
    this._hasFilter = false;
    if (this._bomLines == null)
      return;
    this._bomLines.Clear();
    this._bomLines = (List<BomLine>) null;
  }

  public void DetachAndCleanUpModelData(ref BomLineModel model)
  {
    this.CleanUpFilterData();
    BindingOperations.ClearAllBindings((DependencyObject) this);
    this.DataContext = (object) null;
    if (model != null)
    {
      model.BomLines.Clear();
      model.BomLines = (ObservableCollection<BomLine>) null;
      model.PropertyNiceNames.Clear();
      model.PropertyNiceNames = (ObservableCollection<string>) null;
      model = (BomLineModel) null;
    }
    this.Columns.Clear();
  }

  private string GetValueFromIndex(BomLine item, int columnIndex)
  {
    if (columnIndex == this.FirstStaticColumnIndex)
      return item.Name;
    return columnIndex == this.FirstStaticColumnIndex + 1 ? item.SourceStructureName : item.PropertyValues[columnIndex - this.NumberOfStaticColumns];
  }

  private DataGridColumnHeader GetColumnHeader(DataGridColumn column, DependencyObject depObj)
  {
    for (int childIndex = 0; childIndex < VisualTreeHelper.GetChildrenCount(depObj); ++childIndex)
    {
      DependencyObject child = VisualTreeHelper.GetChild(depObj, childIndex);
      if (child is DataGridColumnHeader columnHeader1 && columnHeader1.Column == column)
        return columnHeader1;
      DataGridColumnHeader columnHeader2 = this.GetColumnHeader(column, child);
      if (columnHeader2 != null)
        return columnHeader2;
    }
    return (DataGridColumnHeader) null;
  }

  private void HandleFilterSet(int columnIndex)
  {
    FilterButton vtDescendant = ADTCExtension.GetVTDescendant<FilterButton>((DependencyObject) this.GetColumnHeader(this.Columns[columnIndex], (DependencyObject) this));
    if (vtDescendant == null)
      return;
    vtDescendant.IsFilterSet = this._hasFilter;
  }

  private void HandleFilterRemoved()
  {
    foreach (FilterButton vtDescendant in ADTCExtension.GetVTDescendants<FilterButton>((DependencyObject) this))
    {
      if (vtDescendant != null)
        vtDescendant.IsFilterSet = this._hasFilter;
    }
  }

  public void Filter(string searchText, int columnIndex)
  {
    BomLineModel dataContext = this.DataContext as BomLineModel;
    ObservableCollection<BomLine> bomLines = dataContext.BomLines;
    if (!this._hasFilter || this._bomLines.Count == 0)
    {
      this._bomLines.Capacity = dataContext.BomLines.Count;
      this._bomLines = dataContext.BomLines.Select<BomLine, BomLine>((Func<BomLine, BomLine>) (a => a)).ToList<BomLine>();
    }
    for (int index = bomLines.Count - 1; index >= 0; --index)
    {
      if (!this.GetValueFromIndex(bomLines[index], columnIndex).ContainsIgnoreCase(searchText))
        bomLines.RemoveAt(index);
    }
    this._hasFilter = dataContext.BomLines.Count < this._bomLines.Count;
    if (!this._hasFilter)
      return;
    this.HandleFilterSet(columnIndex);
  }

  public void RemoveFilter()
  {
    if (!this._hasFilter)
      return;
    this._hasFilter = false;
    BomLineModel model = this.DataContext as BomLineModel;
    if (model.BomLines.Count != this._bomLines.Count)
    {
      model.BomLines.Clear();
      this._bomLines.ForEach((Action<BomLine>) (x => model.BomLines.Add(x)));
    }
    this.HandleFilterRemoved();
  }
}
